﻿using System.Windows.Controls;
using System.Windows.Input;
using SilverlightApplication.UIControls;
using LinqToAmazonSL;
using SilverlightApplication.LanguageResources;

namespace SilverlightApplication
{
    public partial class BookAdvancedPanel : UserControl, ISearchPanelContent
	{
        SearchPanels parent;

		public BookAdvancedPanel(SearchPanels parent)
		{
			// Required to initialize variables
			InitializeComponent();

            asTitle.Text = LanguageLoader.GetString("asTitle");
            asAuthor.Text = LanguageLoader.GetString("asAuthor");
            asOrderBy.Text = LanguageLoader.GetString("asOrderBy");
            asPrice.Text = LanguageLoader.GetString("asPrice");
            rbBookUpward.Content = LanguageLoader.GetString("rbBookUpward");
            rbBookDownward.Content = LanguageLoader.GetString("rbBookDownward");
            asPublisher.Text = LanguageLoader.GetString("asPublisher");
            asKeywords.Text = LanguageLoader.GetString("asKeywords");

            cbItBookPrice.Content = LanguageLoader.GetString("cbItBookPrice");
            cbItBookTitle.Content = LanguageLoader.GetString("cbItBookTitle");
            emptycmb.Content = LanguageLoader.GetString("emptycmbbook");

            cboxItemFrom10to50.Content = LanguageLoader.GetString("cboxItemFrom10to50");
            cboxItemMorethan50.Content = LanguageLoader.GetString("cboxItemMorethan50");
            cboxItemUp10.Content = LanguageLoader.GetString("cboxItemUp10");
            emptycmb1.Content = LanguageLoader.GetString("emptycmbbook");

            this.parent = parent;
		}

        public SearchCriteria GetSearchCriteria()
        {
            SearchCriteria criteria = new SearchCriteria();
            AmazonBook book = new AmazonBook();

            // Begin Advance Books Search
            book.Title = txbAdvBookTitle.Text.Trim();
            book.Author = txbAdvBookAuthor.Text.Trim();
            book.Publisher = txbAdvBookPublisher.Text.Trim();

          
            string orderBy = ((ComboBoxItem)cboxAdvBookOrderBy.SelectedItem).Content.ToString();

            if (orderBy == LanguageLoader.GetString("cbItBookPrice"))
            {
                criteria.OrderBy = OrderClass.Price;
            }
            else if (orderBy == LanguageLoader.GetString("cbItBookTitle"))
            {
                criteria.OrderBy = OrderClass.Title;
            }
            else
            {
                criteria.OrderBy = OrderClass.None;
            }

            criteria.Direction = (bool)rbBookUpward.IsChecked ? OrderDirection.Ascending : OrderDirection.Descending;

            book.Keywords = txbAdvBookKeywords.Text.Trim();

            book.MaximumPrice = "";
            book.MinimumPrice = "";

            if (((ComboBoxItem)cboxPrice.SelectedItem) != null)
            {
                string price = ((ComboBoxItem)cboxPrice.SelectedItem).Content.ToString();
                if (price == LanguageLoader.GetString("cboxItemUp10"))
                {
                    book.MaximumPrice = "1000";
                }
                else if (price == LanguageLoader.GetString("cboxItemFrom10to50"))
                {
                    book.MaximumPrice = "5000";
                    book.MinimumPrice = "1000";
                }
                else if (price == LanguageLoader.GetString("cboxItemMorethan50"))
                {
                    book.MinimumPrice = "5000";
                }
            }

            criteria.Item = book;

            return criteria;
        }

        public bool FieldsValidate()
        {
            return !txbAdvBookTitle.Text.Equals(string.Empty) || !txbAdvBookAuthor.Text.Equals(string.Empty)
                    || !txbAdvBookPublisher.Text.Equals(string.Empty) || !txbAdvBookKeywords.Text.Equals(string.Empty)
                    || ((ComboBoxItem)cboxAdvBookOrderBy.SelectedItem == cbItBookTitle && !txbAdvBookTitle.Text.Equals(string.Empty))
                    || ((ComboBoxItem)cboxAdvBookOrderBy.SelectedItem == cbItBookPrice && (ComboBoxItem)cboxPrice.SelectedItem != emptycmb1)
                    || (ComboBoxItem)cboxAdvBookOrderBy.SelectedItem != emptycmb
                    || (ComboBoxItem)cboxPrice.SelectedItem != emptycmb1;
        }

        public void ClearFields()
        {
            txbAdvBookAuthor.Text = "";
            txbAdvBookPublisher.Text = "";
            txbAdvBookTitle.Text = "";
            txbAdvBookKeywords.Text = "";
            cboxAdvBookOrderBy.SelectedItem = emptycmb;
            cboxPrice.SelectedItem = emptycmb1;
        }

        private void KeyPress(object sender, KeyEventArgs e)
        {
            if (Key.Enter.Equals(e.Key))
            {
                parent.beginSearch();
            }
        }
	}
}